(function($) {
	"use strict"; 

    var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;

	var localCache = {
        data: {},
        remove: function (url) {
            delete localCache.data[url];
        },
        exist: function (url) {
            return localCache.data.hasOwnProperty(url) && localCache.data[url] !== null;
        },
        get: function (url) {
            return localCache.data[url];
        },
        set: function (url, cachedData, callback) {
            localCache.remove(url);
            localCache.data[url] = cachedData;
            if ($.isFunction(callback)) callback(cachedData);
        }
    };

    var noty = function(type, message){
        return new Noty({
            type: type,
            layout: 'topRight',
            theme: 'mint',
            progressBar: true,
            text: message,
            timeout: 4000,
            animation: {
                open: 'animated bounceInRight',
                close: 'animated bounceOutRight'
            }
        }).show();
    }

    var is_email = function(email) {
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
    }

    var custom_content = function(){
        if($('.post-password-form').length > 0){
            $('.post-password-form').addClass('form-inline');
            $('.post-password-form input[name="post_password"]').addClass('form-control');
            $('.post-password-form input[type="submit"]').addClass('ps-btn');
        }

        $('.menu li.menu-item-has-children').addClass('dropdown');
        $('.widget select').addClass('form-control');
        $('.widget_nav_menu ul').removeClass('menu');
        $('.widget_nav_menu ul.sub-menu').addClass('children');
        $('.widget_nav_menu ul').removeClass('sub-menu');
        if($('.mega-sub-menu').length > 0){$('.mega-sub-menu').addClass('sub-menu');}
        
        if($('.mega-menu-megamenu').length > 0){
            $('.mega-menu-megamenu').addClass('has-mega-menu');
            $('.mega-menu-megamenu').removeClass('dropdown');
            $('.mega-menu-megamenu>ul').removeClass('sub-menu');
            $('.mega-menu-megamenu>ul').wrap('<div class="mega-menu"><div class="mega-wrap">');
            $('.mega-menu-megamenu .mega-menu ul.mega-sub-menu>li').addClass('mega-column');
        }

        if(!$('body').hasClass('woocommerce-cart')){$('.ps-post__content table').addClass('table table-striped');}
        $('.ps-widget select').addClass('form-control');
        $('.ps-post__content dl').each(function(){
            if(!$(this).hasClass('gallery-item')){
                $(this).addClass('dl-horizontal');
            }
        });

        $('.ps-block__content table').addClass('table table-striped');
        $('.tab-pane table').wrap('<div class="table-responsive"></div>');
        $('.tab-pane table').addClass('table ps-table--sizeguide');
        $('.ps-block__content dl').addClass('dl-horizontal');

        $('body').on('click', '.quantity span, .qty-changer a', function(){
            var button = $(this),
            parent = $(this).closest('.quantity'),
            input = parent.find('input'),
            number = parseInt(input.val());

            if(number >= 1){
                if(button.hasClass('up')){
                    input.val(parseInt(number + 1)).trigger('change');
                }else{
                    if(number > 1){input.val(parseInt(number - 1)).trigger('change');};
                }
            }else{
               if(button.hasClass('up')){
                    input.val(parseInt(number + 1)).trigger('change');
                }else{
                    if(number > 1){input.val(parseInt(number)).trigger('change');};
                } 
            }
            $('.woocommerce-cart .quantity .qty').trigger('change');
            return false;
        });

        $('.ps-post--2').each(function(){
            var img = $(this).find('.wp-post-image'),
            width = img.width(),
            height = img.height(),
            overlay = $(this).find('.ps-post__thumbnail .ps-post__overlay');
            overlay.css('width', width);
            overlay.css('height', height);
        });
        $('.widget_tag_cloud ul').removeClass('ps-list--arrow');
        $('ul.ps-list--checked').removeClass('ps-list--arrow');
        $('ul.ps-list--color').removeClass('ps-list--arrow');
        $('.widget_tag_cloud').addClass('widget_tags');
        $('.widget_tag_cloud .tag-cloud-link').removeAttr('style');
        $('.widget_price_filter button').addClass('ps-btn');
        $('.widget_price_filter form').wrap('<div class="ps-widget__content"></div>');

        $('[data-color]').each(function() {
            if ($(this).attr('data-color')) {
                var color = $(this).attr('data-color');
                $(this).css({
                    'background-color': ''+ color +''
                });
            }
        });

        $('.ps-widget--footer ul').addClass('ps-list--line');
        $('.widget_recent_entries').addClass('widget_categories');
        $('.widget_recent_comments').addClass('widget_categories');
        $('.widget_archive').addClass('widget_categories');
        $('.widget_meta').addClass('widget_categories');
        $('.widget_pages').addClass('widget_categories');
        $('.widget_rss').addClass('widget_categories');
        $('.widget_nav_menu').addClass('widget_categories');
        $('.widget table').addClass('table table-bordered');

        $('.ps-widget--info').each(function(i) {
            if(i > 0){
                $(this).addClass('second');
            }
        });

        $('.ps-hotspot a').each(function(){
            if($(this).data('left') != ''){
                $(this).css('left', $(this).data('left'));
            }

            if($(this).data('right') != ''){
                $(this).css('right', $(this).data('right'));
            }

            if($(this).data('bottom') != ''){
                $(this).css('bottom', $(this).data('bottom'));
            }

            if($(this).data('top') != ''){
                $(this).css('top', $(this).data('top'));
            }
        });

        if($('.ps-features').length > 0){
            $('.ps-features').each(function(){
                if($(this).hasClass('bg--cover')){
                    $('.ps-features').find('.ps-iconbox').addClass('ps-iconbox--inverse');
                }
            });
        }

        $('#comments .comment').each(function(i) {
            var child = $(this).find('.ps-comment--reply');
            if(child.length > 0){
                $(this).addClass('comment-has-child');
            }
        });

        $('.mc4wp-form input[type="email"]').addClass('form-control');
        $('.mc4wp-form input[type="submit"]').addClass('ps-btn');
        $('.ps-form--subscribe input[type="submit"]').val(nouexist_theme_params.button_newsletter_text);
        $('.ps-form--subscribe input[type="submit"]').addClass('ps-btn--black');

        $('.portfolio-meta a').removeAttr('href');
        $('body').on('click', '.reset_variations', function(){
            var form = $(this).closest('form');
            form.find('select').val('').change();
            return false;
        });
        
        $('.ps-columns .ps-collection-column').wrap('<div class="ps-column"></div>');
    }

	var share_post = function(){

		$('body').on('click', '.exist-share a', function(){
            var element = $(this),
            type = $(this).data('type'),
            message = $(this).closest('.exist-share').data('title'),
            url = $(this).closest('.exist-share').data('url'),
            share_url = '';

            if (typeof type === 'undefined' || !type || type == null){
                type = $(this).data('type');
            }
            if (typeof message === 'undefined' || !message || message == null){
                message = $(this).data('title');
            }
            if (typeof url === 'undefined' || !url || url == null){
                url = $(this).data('url');
            }

            switch (type) {

                case 'facebook':
                    share_url += 'http://www.facebook.com/sharer.php?u=' + url + '&t=' + encodeURIComponent(message);
                    break;
                case 'google':
                    share_url += 'https://plus.google.com/share?url=' + encodeURIComponent(message + ' ' + url);
                    break;
                case 'twitter':
                    share_url += 'https://twitter.com/home/?status=' + encodeURIComponent(message + ' ' + url);
                    break;
                case 'reddit':
                    share_url += 'http://reddit.com/submit?url=' + url + '&title=' + encodeURIComponent(message);
                    break;
                case 'linkedin':
                    share_url += 'http://linkedin.com/shareArticle?mini=true&url=' + url + '&title=' + encodeURIComponent(message);
                    break;
                case 'tumblr':
                    share_url += 'http://www.tumblr.com/share/link?url=' + url + '&name=' + encodeURIComponent(message);
                    break;
                case 'pinterest':
                    share_url += 'http://pinterest.com/pin/create/button/?url=' + url + '&title=' + encodeURIComponent(message);
                    break;
                case 'vk':
                    share_url += 'http://vkontakte.ru/share.php?url=' + url + '&title=' + encodeURIComponent(message);
                    break;
                default:
                    share_url += 'mailto:?subject='+ message +'&body=' + url + '';
                    break;
            }

            window.open(share_url, '_blank');

            return false;
        });
	}

    var post_like = function(){
        if( $('.post-like').length > 0 ){

            $('body').on('click', '.post-like', function(){
              var button = $(this),
              wrap = $(this).closest('.ps-post__actions'),
              data = {
                post_id: button.data('id'),
                security: nouexist_theme_params.ajax_nonce,
                action: 'nouexist_helpers_post_like'
              };

              if(button.data('id') != ''){

                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        button.remove();
                        wrap.find('.share-posts').before(data.button);
                    },
                    error     : function(jqXHR, textStatus, errorThrown) {
                        alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                    }

                });

              }

              return false;

            });

        }
    }

    var product_wishlist = function(){
        if( $('.product a.wishlist').length > 0 ){

            $('body').on('click', '.product a.wishlist', function(){

                var do_action = 'add',
                message = '';

                if($(this).hasClass('delete-wishlist')){
                    do_action = 'delete';
                }

                var button = $(this),
                product = $(this).closest('.product'),
                title = product.data('title'),
                url = product.data('url'),
                data = {
                    product_id: button.data('product_id'),
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: 'nouexist_helpers_wishlist',
                    do_action: do_action
                  };
                if(button.hasClass('added')){
                    window.location.href = nouexist_theme_params.wishlist_page_url;
                    return false;
                }

                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        message = '"<a href="'+ url +'"><strong>'+ title +'</strong></a>" ' + data.html;
                        if(button.hasClass('delete-wishlist')){
                            product.remove();
                            if(data.status == '1'){
                                noty('success', message);
                            }else{
                                noty('error', message);
                            }
                        }else{
                            button.addClass('added');
                            if(data.status == '1'){
                                button.find('i').removeAttr('class');
                                button.find('i').addClass('fa fa-check');
                                noty('success', message);
                            }else{
                                noty('error', message);
                            }
                            setTimeout(
                                function(){ 
                                    button.find('i').removeAttr('class');
                                    button.find('i').addClass('exist-heart');
                                }, 3000);
                        }
                    },
                    error     : function(jqXHR, textStatus, errorThrown) {
                        alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                    }
                });

                return false;

            });

        }
    }

    var product_compare = function(){
        if( $('.product a.compare').length > 0 ){

            $('body').on('click', '.product a.compare', function(){

                var do_action = 'add', message = '';

                if($(this).hasClass('delete-compare')){
                    do_action = 'delete';
                }

                var button = $(this),
                product = $(this).closest('.product'),
                title = product.data('title'),
                url = product.data('url'),
                data = {
                    product_id: button.data('product_id'),
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: 'nouexist_helpers_compare',
                    do_action: do_action
                  };

                if(button.hasClass('added')){
                    window.location.href = nouexist_theme_params.compare_page_url;
                    return false;
                }

                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        button.addClass('added');
                        message = '"<a href="'+ url +'"><strong>'+ title +'</strong></a>" ' + data.html;

                        if(button.hasClass('delete-compare')){
                            if(data.status == '1'){
                                product.remove();
                                noty('success', message);
                            }else{
                                noty('error', message);
                            }
                        }else{
                            if(data.status == '1'){
                                button.find('i').removeAttr('class');
                                button.find('i').addClass('fa fa-check');

                                noty('success', message);
                            }else{
                                noty('error', message);
                            }
                            setTimeout(function(){ 
                                button.find('i').removeAttr('class');
                                button.find('i').addClass('exist-compare');
                            }, 3000);
                        }
                    },
                    error     : function(jqXHR, textStatus, errorThrown) {
                        alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                    }
                });

                return false;

            });

        }
    }

    var product_added_to_cart = function(){

        $('body').on('added_to_cart', function(e, fragments, cart_hash, button){
            var product = $(button).closest('.product'),
            title = product.data('title'),
            url = product.data('url'),
            message = '"<a href="'+ url +'"><strong>'+ title +'</strong></a>" ' + nouexist_theme_params.added_to_cart_text;
            
            if($(button).hasClass('remove-after-added')){
                var product_id = $(button).data('product_id'),
                type = $(button).data('type'),
                data = {
                    product_id: product_id,
                    type: type,
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: 'nouexist_helpers_remove_product_from_lists'
                  };
                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                      product.remove();
                    },
                });
            }

            noty('success', message);
        });
    }

    var newsletter = function(){
        if($('.nouexist-subscribe').length > 0){

            $('.nouexist-subscribe').on('click', '.btn-fake-subscribe', function(){
                var btn = $(this),
                section = $(this).closest('.nouexist-subscribe'),
                mc4wp_form = section.find('form.mc4wp-form'),
                fake_email = section.find('form.nouexist-subscribe-form input[name="your_email"]').val();

                if(fake_email != ''){
                    if(!is_email(fake_email)){
                        noty('error', nouexist_theme_params.email_invalid_text);
                        return false;
                    }else{
                        mc4wp_form.find('input[name="EMAIL"]').val(fake_email);
                        mc4wp_form.submit();
                        return false;
                    }
                }else{
                    noty('error', nouexist_theme_params.required_text);
                }
                return false;
            });

            $('.nouexist-subscribe').each(function(){
                var message = $(this).find('.mc4wp-response').html();
                $(this).find('.nouexist-subscribe-form .nouexist-subscribe-message').html(message);
            });
            
        }
    }

    var contact = function(){
        $('.nouexist-contact__submit').on('click', function(){
            var $this = $(this),
            form = $(this).closest('form'),
            name = $('input[name="name"]').val(),
            email = $('input[name="email"]').val(),
            content = $('textarea[name="content"]').val(),
            submit = true;

            if(name == '' || email == '' || content == ''){
                form.find('.form-control').each(function(){
                    var val = $(this).val();
                    if(val == ''){
                        $(this).closest('.form-group').addClass('has-error');
                    }else{
                        $(this).closest('.form-group').removeClass('has-error');
                    }
                });
                submit = false;
                noty('error', nouexist_theme_params.required_text);
                return false;
            }

            if(!is_email(email)){
                noty('error', nouexist_theme_params.email_invalid_text);
                submit = false;
                return false;
            }

            if(submit){
                var data = {
                    action: 'nouexist_helpers_email_contact',
                    nonce:  nouexist_theme_params.ajax_nonce,
                    name: name,
                    email: email,
                    content: content,
                };
                $.ajax({
                    type:    'POST',
                    url:     nouexist_theme_params.ajax_url,
                    data:    data,
                    beforeSend: function(){
                        $this.html(nouexist_theme_params.btn_contact_text + ' <i class="fa fa-spin fa-spinner" aria-hidden="true"></i>');
                    },
                    success: function( data ) {console.log(data);
                        form.trigger("reset");
                        $this.html(nouexist_theme_params.btn_contact_text + ' <i class="ps-icon-next"></i>');
                        if ( data.status == 1 ) {
                            noty('success', nouexist_theme_params.send_email_success_text);
                        }else{
                            noty('error', nouexist_theme_params.send_email_fail_text);
                        }
                    }
                });
            }
            return false;
        });
    }

    function mapConfig() {
        $('.nouexist-google-map').each(function(i){
            $(this).attr('id', 'nouexist-google-map-'+i);
            var map = $('#nouexist-google-map-'+i);
            map.lazyLoadGoogleMaps({
                key: nouexist_theme_params.map_api,
                libraries: 'places',
                region: 'GB',
                callback: function( container, map ){
                    var id_map = $('#nouexist-google-map-'+i),
                    map_zoom_control = false,
                    icon = $('#nouexist-google-map-'+i).data('icon'),
                    icon_url = '',
                    address_type = 'address';
                    if ( id_map.length){

                        if( typeof icon === 'undefined' || icon === null ){
                            icon_url = nouexist_theme_params.map_marker;
                        }else{
                            icon_url = icon;
                        }

                        var address = id_map.data('address');
                        address_type = id_map.data('type');
                        var geocoder = new google.maps.Geocoder();
                        
                        if(address_type == 'address'){
                            geocoder.geocode( { 'address': address }, function(results, status) {
                                
                                if (status == google.maps.GeocoderStatus.OK) {

                                    var lat = results[0].geometry.location.lat();
                                    var lng = results[0].geometry.location.lng();

                                } else {

                                    var lat = '39.0928745';
                                    var lng = '-94.5763632';
                                }


                                var latlng = new google.maps.LatLng(lat,lng);

                                /* Map Option */
                                var mapOptions = {
                                    zoom: id_map.data('zoom'),
                                    center: latlng,
                                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                                    scrollwheel: map_zoom_control,
                                    draggable: map_zoom_control,
                                    panControl: map_zoom_control,
                                    zoomControl: map_zoom_control,
                                    mapTypeControl: map_zoom_control,
                                    scaleControl: map_zoom_control,
                                    streetViewControl:false,
                                    overviewMapControl:false,
                                    zoomControlOptions: {
                                      style:google.maps.ZoomControlStyle.SMALL
                                    },
                                    styles: [
                                        {
                                            "featureType": "administrative",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "saturation": "-100"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "administrative.province",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "visibility": "off"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "landscape",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "saturation": -100
                                                },
                                                {
                                                    "lightness": 65
                                                },
                                                {
                                                    "visibility": "on"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "poi",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "saturation": -100
                                                },
                                                {
                                                    "lightness": "50"
                                                },
                                                {
                                                    "visibility": "simplified"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "road",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "saturation": "-100"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "road.highway",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "visibility": "simplified"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "road.arterial",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "lightness": "30"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "road.local",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "lightness": "40"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "transit",
                                            "elementType": "all",
                                            "stylers": [
                                                {
                                                    "saturation": -100
                                                },
                                                {
                                                    "visibility": "simplified"
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "water",
                                            "elementType": "geometry",
                                            "stylers": [
                                                {
                                                    "hue": "#ffff00"
                                                },
                                                {
                                                    "lightness": -25
                                                },
                                                {
                                                    "saturation": -97
                                                }
                                            ]
                                        },
                                        {
                                            "featureType": "water",
                                            "elementType": "labels",
                                            "stylers": [
                                                {
                                                    "lightness": -25
                                                },
                                                {
                                                    "saturation": -100
                                                }
                                            ]
                                        }
                                    ]
                                };

                                /* Create Map*/
                                var map = new google.maps.Map(document.getElementById('nouexist-google-map-'+i), mapOptions);
                                
                                if( icon_url == '' ){

                                  var marker_options = {
                                      position: latlng,
                                      map: map,
                                      title: id_map.data('title'),
                                      draggable: false,
                                      
                                  };

                                }else{

                                  var marker_options = {
                                      position: latlng,
                                      map: map,
                                      title: id_map.data('title'),
                                      draggable: false,
                                      icon: {url: icon_url},
                                      animation: google.maps.Animation.BOUNCE
                                      
                                  };

                                }
                                var infowindow = new google.maps.InfoWindow({
                                    content: ""
                                });
                                var marker =  new google.maps.Marker(marker_options);
                                marker.addListener('click', function() {
                                    infowindow.open(map, marker);
                                    infowindow.setContent(id_map.data('title'));
                                });
                            });
                        }else{
                            var latlng = new google.maps.LatLng(id_map.data('lat'),id_map.data('lng'));
                            /* Map Option */
                            var mapOptions = {
                                zoom: id_map.data('zoom'),
                                center: latlng,
                                mapTypeId: google.maps.MapTypeId.ROADMAP,
                                scrollwheel: map_zoom_control,
                                draggable: map_zoom_control,
                                panControl: map_zoom_control,
                                zoomControl: map_zoom_control,
                                mapTypeControl: map_zoom_control,
                                scaleControl: map_zoom_control,
                                streetViewControl:false,
                                overviewMapControl:false,
                                zoomControlOptions: {
                                  style:google.maps.ZoomControlStyle.SMALL
                                },
                                styles: [
                                    {
                                        "featureType": "administrative",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "saturation": "-100"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "administrative.province",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "visibility": "off"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "landscape",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "saturation": -100
                                            },
                                            {
                                                "lightness": 65
                                            },
                                            {
                                                "visibility": "on"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "poi",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "saturation": -100
                                            },
                                            {
                                                "lightness": "50"
                                            },
                                            {
                                                "visibility": "simplified"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "road",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "saturation": "-100"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "road.highway",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "visibility": "simplified"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "road.arterial",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "lightness": "30"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "road.local",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "lightness": "40"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "transit",
                                        "elementType": "all",
                                        "stylers": [
                                            {
                                                "saturation": -100
                                            },
                                            {
                                                "visibility": "simplified"
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "water",
                                        "elementType": "geometry",
                                        "stylers": [
                                            {
                                                "hue": "#ffff00"
                                            },
                                            {
                                                "lightness": -25
                                            },
                                            {
                                                "saturation": -97
                                            }
                                        ]
                                    },
                                    {
                                        "featureType": "water",
                                        "elementType": "labels",
                                        "stylers": [
                                            {
                                                "lightness": -25
                                            },
                                            {
                                                "saturation": -100
                                            }
                                        ]
                                    }
                                ]
                            };

                            /* Create Map*/
                            var map = new google.maps.Map(document.getElementById('nouexist-google-map-'+i), mapOptions);
                            
                            if( icon_url == '' ){

                              var marker_options = {
                                  position: latlng,
                                  map: map,
                                  title: id_map.data('title'),
                                  draggable: false,
                                  
                              };

                            }else{

                              var marker_options = {
                                  position: latlng,
                                  map: map,
                                  title: id_map.data('title'),
                                  draggable: false,
                                  icon: {url: icon_url},
                                  animation: google.maps.Animation.BOUNCE
                                  
                              };

                            }
                            var infowindow = new google.maps.InfoWindow({
                                content: ""
                            });
                            var marker =  new google.maps.Marker(marker_options);
                            marker.addListener('click', function() {
                                infowindow.open(map, marker);
                                infowindow.setContent(id_map.data('title'));
                            });
                        }

                    }
                }
            });
        });
    }

    var load_more_products = function(){
        
        var nextpage = 2;

        $('.woocommerce').on('click', '.btn-loadmore', function(){
            var btn = $(this),
            maxpage = $(this).data('maxpage'),
            maxposts = $(this).data('posts'),
            columns = $(this).data('col'),
            shortcode = '',
            ids = '',
            shortcode_id = '',
            cat = '',
            order_by = '',
            query_type = '',
            template = 'products_tmp',
            parent = $(this).closest('.nouexist-products'),
            style = '',
            posts_per_page = '';

            var action = 'nouexist_helpers_load_more_products';

            if(btn.hasClass('shortcode')){
                action = 'nouexist_helpers_load_more_products_shortcode';
                shortcode = btn.data('shortcode'),
                ids = btn.data('ids'),
                cat = btn.data('cat'),
                order_by = btn.data('order_by'),
                query_type = btn.data('query'),
                template = btn.data('template'),
                style = btn.data('style'),
                shortcode_id = btn.data('shortcodeid'),
                posts_per_page = btn.data('perpage');
            }

            if(nextpage <= maxpage){
                btn.html('<i class="fa fa-spin fa-spinner" aria-hidden="true"></i> ' + nouexist_theme_params.load_more_text);

                var data = {
                    page: nextpage,
                    columns: columns,
                    shortcode: shortcode,
                    ids: ids,
                    shortcode_id: shortcode_id,
                    cat: cat,
                    order_by: order_by,
                    query_type: query_type,
                    number: posts_per_page,
                    style: style,
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: action
                  };

                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        nextpage++;
                        btn.text(nouexist_theme_params.load_more_text);
                        if(data.status == '1'){
                            if(parent.find('.products').hasClass('ps-masonry')){
                                $('#'+template).append(data.html);
                                var $elems = [];
                                $('#'+ template +' .grid-item').each(function(){
                                    var e = $(this);
                                    $elems.push(e[0]);
                                });
                                parent.find('.ps-masonry').imagesLoaded( function() {
                                    parent.find('.ps-masonry').append( $elems ).masonry( 'appended', $elems, true );
                                });
                            }else{
                                parent.find('.products').append(data.html);
                                parent.find('.ps-product--1').matchHeight('remove').matchHeight();
                                setTimeout(function() {
                                    $.fn.matchHeight._apply(parent.find('.ps-product--1'));
                                }, 300);
                            }
                            shortcode_products_filter();
                        }
                    },
                });

            }else{
                btn.text(nouexist_theme_params.no_more_products);
            }

            return false;
        });
    }


    var load_more_portfolios = function(){
        
        var nextpage = 2;

        $('.ps-portfolio').on('click', '.btn-loadmore', function(){
            var btn = $(this),
            maxpage = $(this).data('maxpage'),
            col = $(this).data('col'),
            per_page = $(this).data('perpage'),
            maxposts = $(this).data('posts');

            if(nextpage <= maxpage){
                btn.html('<i class="fa fa-spin fa-spinner" aria-hidden="true"></i> ' + nouexist_theme_params.load_more_text);

                var data = {
                    page: nextpage,
                    col: col,
                    per_page: per_page,
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: 'nouexist_helpers_load_more_portfolios'
                  };
                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        nextpage++;
                        btn.text(nouexist_theme_params.load_more_text);
                        if(data.status == '1'){
                            $('#ps-portfolio-tmp').append(data.html);
                            var $elems = [];
                            $('#ps-portfolio-tmp .grid-item').each(function(){
                                var e = $(this);
                                $elems.push(e[0]);
                            });
                            $('.ps-masonry').imagesLoaded( function() {
                                $('.ps-masonry').append( $elems ).masonry( 'appended', $elems, true );
                            });
                        }
                    },
                });

            }else{
                btn.text(nouexist_theme_params.no_more_projects);
            }

            return false;
        });
    }

    var shortcode_products_filter = function(){
        if($('.ps-tab-filter').length > 0){
            $('.ps-tab-filter').on('click', '.ps-filter__categories a', function(){
                var parent = $(this).closest('.ps-tab-filter'),
                cat = $(this).closest('li').attr('class');

                parent.find('li').removeClass('current');
                $(this).closest('li').addClass('current');
                if(cat == 'all'){
                    parent.find('.products .product').show();  
                }else{
                    parent.find('.products .product').hide();
                    parent.find('.products .'+cat).show();    
                }
                
                return false;
            });
        }
    }

    var remove_cart_header = function(){
        if($('body .header__cart').length > 0){
            $('body').on('click', '.header__cart a.ps-product__remove', function(){
                var btn = $(this),
                data = {
                    product_id: $(this).data('product_id'),
                    cart_item_key: $(this).data('cart_item_key'),
                    nonce: nouexist_theme_params.ajax_nonce,
                    action: 'nouexist_helpers_remove_product_from_cart'
                  };
                $.ajax({
                    type       : "POST",
                    data       : data,
                    url        : nouexist_theme_params.ajax_url,
                    success    : function(data){
                        if(data.status == '1'){console.log(data.html);
                            $('.header__cart').html(data.html);
                            noty('success', nouexist_theme_params.remove_from_cart_text);  
                        }
                    },
                });

                return false;
            });
        }
    }

	$(document).ready(function() {
        contact();
        mapConfig();
        newsletter();
        share_post();
        custom_content();
        post_like();
        product_wishlist();
        product_compare();
        product_added_to_cart();
        load_more_products();
        load_more_portfolios();
        shortcode_products_filter();
        remove_cart_header();
        if( $(window).width() > 1012){
            if($('.main-menu').length > 0){
                if($('.main-menu li').length > 5){
                    $('.main-menu').children('li').addClass('menu-mr-1');
                }

                $('.main-menu').children('li').each(function(i){
                    if(i > 4){
                        $(this).addClass('menu-mt--25');
                        $(this).children('a').css('line-height', '1.8em');
                    }
                });
            }
        }

    });
	
})(jQuery);